BALL = {0, 0}
SPEED = 400
CURXY = {}
B1_ST = 1
B2_ST = 1
BST = {{1, 1, 1, 1, 1}, {1, 1}}
b1 = {}
b2 = {}
WIN = 0
BTN = {{}, {}}
BTCOLOR = {{0.9, 0.9, 0.9}, {0.7, 0.7, 0.7}, {0.5, 0.5, 0.5}}
BTS = {{'red', 'green', 'shit', 'white'}, {'yes', 'no'}}
BLCR = {1, 1, 1}

function CreateButton(id, x, y, w, h, word)
    table.insert(BTN[id], {x + 400, y + 300, w, h, word})
end

function Oput(word, x, y, size)
    love.graphics.setColor(0.2, 0.3, 0.3)
    love.graphics.setBlendMode("alpha")
    local font = love.graphics.newFont(size)
    love.graphics.setFont(font)
    love.graphics.print(word, x, y + size)
end


function love.load()
    b1[1] = love.graphics.newImage("button.jpg")
    b1[2] = love.graphics.newImage("button_up.jpg")
    b1[3] = love.graphics.newImage("button_down.jpg")
    b2[1] = love.graphics.newImage("exit.jpg")
    b2[2] = love.graphics.newImage("exit_up.jpg")
    b2[3] = love.graphics.newImage("exit_down.jpg")
    CreateButton(1, -250, -220, 200, 100, 'red')
    CreateButton(1, 50, -220, 200, 100, 'green')
    CreateButton(1, -250, -40, 200, 100, 'shit')
    CreateButton(1, 50, -40, 200, 100, 'white')
    CreateButton(1, -100, 140, 200, 100, 'exit')
    CreateButton(2, -250, 0, 200, 100, 'yes')
    CreateButton(2, 50, 0, 200, 100, 'no')
end

function love.draw()
    -- background
    love.graphics.setColor(0.9, 0.9, 0.7)
    love.graphics.rectangle("fill", 0, 0, 800, 600)

    -- button 1
    love.graphics.draw(b1[B1_ST], 50, 100)

    -- button 2
    love.graphics.draw(b2[B2_ST], 50, 372) -- 600 - 100 - 128 = 372

    -- stage boarder
    love.graphics.setColor(0.7, 0.8, 0.5)
    love.graphics.setLineWidth(8)
    love.graphics.rectangle("line", 350, 100, 400, 400)

    -- stage
    love.graphics.setColor(BLCR[1], BLCR[2], BLCR[3])
    love.graphics.rectangle("fill", 350, 100, 400, 400)

    -- ball
    love.graphics.setColor(0, 0, 1)
    love.graphics.circle("fill", BALL[1] + 550, BALL[2] + 300, 8)


--[[
################################################
################################################
############    SHIT    WARNING    #############
################################################
################################################
]]


    -- menu
    if WIN ~= 0 then
        for i, v in ipairs(BTN[WIN]) do
            love.graphics.setColor(BTCOLOR[BST[WIN][i]][1], BTCOLOR[BST[WIN][i]][2], BTCOLOR[BST[WIN][i]][3])
            love.graphics.rectangle("fill", v[1], v[2], v[3], v[4])
            love.graphics.setColor(0, 0, 0)
            Oput(v[5], 70 + v[1], v[2], 36)
        end
    end
end

function OnButton(cx, cy, i, id)
    if cx >= BTN[i][id][1] and cx <= BTN[i][id][1] + BTN[i][id][3] and cy >= BTN[i][id][2] and cy <= BTN[i][id][2] + BTN[i][id][4] then
        if love.mouse.isDown(1) then
            return true
        end
    end
    return false
end

function OnButtonUnpressed(cx, cy, i, id)
    if cx >= BTN[i][id][1] and cx <= BTN[i][id][1] + BTN[i][id][3] and cy >= BTN[i][id][2] and cy <= BTN[i][id][2] + BTN[i][id][4] then
        if love.mouse.isDown(1) == false then
            return true
        end
    end
    return false
end





function love.update(dt)
    -- keyboard
    local dir = {0, 0}
    if love.keyboard.isDown("left") then
        dir[1] = dir[1] - SPEED * dt
    end
    if love.keyboard.isDown("right") then
        dir[1] = dir[1] + SPEED * dt
    end
    if love.keyboard.isDown("up") then
        dir[2] = dir[2] - SPEED * dt
    end
    if love.keyboard.isDown("down") then
        dir[2] = dir[2] + SPEED * dt
    end
    BALL[1] = BALL[1] + dir[1]
    BALL[2] = BALL[2] + dir[2]
    if BALL[1] < -192 then
        BALL[1] = -192
    end
    if BALL[1] > 192 then
        BALL[1] = 200 - 8
    end
    if BALL[2] < -200 + 8 then
        BALL[2] = -200 + 8
    end
    if BALL[2] > 200 - 8 then
        BALL[2] = 200 - 8
    end

    -- mouse
    CURXY[1] = love.mouse.getX()
    CURXY[2] = love.mouse.getY()
    if WIN == 0 and CURXY[1] <= 256 + 50 and CURXY[1] >= 50 and CURXY[2] <= 128 + 100 and CURXY[2] >= 100 then
        B1_ST = 2
        if love.mouse.isDown(1) then
            B1_ST = 3
            WIN = 1
        end
    else
        B1_ST = 1
    end
    if WIN == 0 and CURXY[1] <= 256 + 50 and CURXY[1] >= 50 and CURXY[2] <= 128 + 372 and CURXY[2] >= 372 then
        B2_ST = 2
        if love.mouse.isDown(1) then
            B2_ST = 3
            WIN = 2
        end
    else
        B2_ST = 1
    end
    if WIN == 1 then
        if OnButton(CURXY[1], CURXY[2], WIN, 5) then
            WIN = 0
        elseif OnButton(CURXY[1], CURXY[2], WIN, 1) then
            BLCR[1] = 0.5
            BLCR[2] = 0
            BLCR[3] = 0
        elseif OnButton(CURXY[1], CURXY[2], WIN, 2) then
            BLCR[1] = 0
            BLCR[2] = 0.5
            BLCR[3] = 0
        elseif OnButton(CURXY[1], CURXY[2], WIN, 3) then
            BLCR[1] = 0.5
            BLCR[2] = 0.5
            BLCR[3] = 0
        elseif OnButton(CURXY[1], CURXY[2], WIN, 4) then
            BLCR[1] = 1
            BLCR[2] = 1
            BLCR[3] = 1
        end
    end
    if WIN == 2 then
        if OnButton(CURXY[1], CURXY[2], WIN, 2) then
            WIN = 0
        elseif OnButton(CURXY[1], CURXY[2], WIN, 1) then
            os.exit()
        end
    end

    -- button menu color
    if WIN ~= 0 then
        for i in ipairs(BST[WIN]) do
            if OnButtonUnpressed(CURXY[1], CURXY[2], WIN, i) then
                BST[WIN][i] = 2
            elseif OnButton(CURXY[1], CURXY[2], WIN, i) then
                BST[WIN][i] = 3
            else
                BST[WIN][i] = 1
            end
        end
    end
end

function love.keypressed(key)
    
end